package core.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Map;
import java.util.UUID;

public class JWTUtils {
    /**
     * ⽣成令牌
     *
     * @param claims 数据
     * @param secret 密钥
     * @return 令牌
     */
    public static String createToken(Map<String, Object> claims, String secret) {
        String token = Jwts.builder().setClaims(claims)
                .signWith(SignatureAlgorithm.HS512, secret).compact();
        return token;
    }

    /**
     * 从令牌中获取数据
     *
     * @param token  令牌
     * @param secret 密钥
     * @return 数据
     */
    public static Claims parseToken(String token, String secret) {
        return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
    }

    public static String getUserId(Claims claims) {
        return (claims.get("userId")).toString();
    }

    public static String getUserId(String token, String secret) {
        return (parseToken(token, secret).get("userId")).toString();
    }
    public static void main(String[] args) {
//        String secret = "5318ec9c8b66410d9059de5a9bb292ec";
//        System.out.println(secret.length());
//        Map<String, Object> claims = Map.of("username", "zhangsan", "password", "123456");
//        String token = createToken(claims, secret);
//        System.out.println(token);
//        Claims claims1 = parseToken(token, secret);
//        System.out.println(claims1);
//        System.out.println(claims1 == null);
        UUID uuid1 = UUID.randomUUID();
        UUID uuid2 = UUID.randomUUID();
        UUID uuid3 = UUID.randomUUID();
        StringBuffer sb = new StringBuffer();
        String s1 = uuid1.toString().replace("-", "");
        String s2 = uuid2.toString().replace("-", "");
        String s3 = uuid3.toString().replace("-", "");
        sb.append(s1).append(s2).append(s3);
        System.out.println(sb.toString());
        System.out.println(sb.length());
    }
}
